package fi.honkalampisaatio.reaktiopeli.game;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import fi.honkalampisaatio.game.game.LanguageResource;
import fi.honkalampisaatio.game.ui.ScalableIconButton;
import fi.honkalampisaatio.game.ui.ScalableIconLabel;
import fi.honkalampisaatio.reaktiopeli.setup.SetupEvent;
import fi.honkalampisaatio.reaktiopeli.setup.SetupEvent.MESSAGE;
import fi.honkalampisaatio.reaktiopeli.setup.SetupEventListener;

public class GameInstructionsPanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = 2810215912007832132L;

	private SetupEventListener listener;
	
	private ScalableIconLabel buttonsLabel;
	
	private ScalableIconButton cancelButton;
	private ScalableIconButton okButton;
	private ScalableIconButton setupButton;

	private ButtonsPanel buttonImages;

	private LanguageResource dictionary; 


	public GameInstructionsPanel() {
		dictionary = LanguageResource.getInstance();

		//buttons
		buttonsLabel = new ScalableIconLabel(dictionary.get("BUTTONLABEL_TEXT"),
				"./settings/images/Käytettävät painikkeet kuvake.png");
		buttonsLabel.setHorizontalAlignment(JLabel.CENTER);
		this.add(buttonsLabel);
		buttonImages = new ButtonsPanel();
		this.add(buttonImages);

		cancelButton = new ScalableIconButton(dictionary.get("CANCELBUTTON_TEXT"), "./settings/images/Takaisin.png");
		cancelButton.addActionListener(this);
		this.add(cancelButton);
		
		okButton = new ScalableIconButton(dictionary.get("OKBUTTON_TEXT"), "./settings/images/Hyväksy painike.png");
		okButton.setBackground(new Color(0, 127, 14));
		okButton.addActionListener(this);
		this.add(okButton);
		
		setupButton = new ScalableIconButton(dictionary.get("SETUPBUTTON_TEXT"), "./settings/images/Asetukset.png");
		setupButton.addActionListener(this);
		this.add(setupButton);
		
		setComponentsBounds();
	}



	public void addSetupEventListener(SetupEventListener listener) {
		this.listener = listener;
	}


	public void removeSetupEventListener(SetupEventListener listener) {
		if(this.listener==listener) {
			listener=null;
		}
	}
	
	private void fireSetupEvent(SetupEvent e) {
		if(listener!=null) {
			listener.setupEventOccurred(e);
		}
	}
	
	public void setBackButtonIcon(String backbuttonImg)
	{
		if(backbuttonImg!=null && !backbuttonImg.isEmpty())
		{
			try
			{
				cancelButton.setIcon(new ImageIcon(backbuttonImg));
			}
			catch(Exception e)
			{
				System.err.println("Could not create back button icon " + backbuttonImg);
			}
		}
	}
	
	public void setForeground(Color color)
	{
		super.setForeground(color);
		Component[] compList = this.getComponents();
		for(int i=0;i<compList.length;i++)
		{
			compList[i].setForeground(color);
		}
	}

	public void setFont(Font font)
	{
		super.setFont(font);
		Component[] compList = this.getComponents();
		for(int i=0;i<compList.length;i++)
		{
			compList[i].setFont(font);
		}
	}
	
	public void setButtonImages(ArrayList<BufferedImage> orgBtnImages) {
		buttonImages.clearImages();
		if(orgBtnImages!=null) {
			for(int i=0; i<orgBtnImages.size(); i++)
			{
				buttonImages.addImage(orgBtnImages.get(i));
			}
		}	
		buttonImages.repaint();
	}

	public void refreshLanguage()
	{
		buttonsLabel.setText(dictionary.get("BUTTONLABEL_TEXT"));
		okButton.setText(dictionary.get("OKBUTTON_TEXT"));
		cancelButton.setText(dictionary.get("CANCELBUTTON_TEXT"));
		setupButton.setText(dictionary.get("SETUPBUTTON_TEXT"));
	}
	
	protected void resizeTexts()
	{
		float fontSize = 144;
		FontMetrics fm;
		Component[] compList = this.getComponents();
		for(int i=0;i<compList.length;i++)
		{
			if(compList[i] instanceof JLabel)
			{
				JLabel lbl = (JLabel)compList[i];
				fm = lbl.getFontMetrics(lbl.getFont().deriveFont(fontSize));
				if(fm.getHeight()>lbl.getHeight())
				{
					fontSize = fontSize * lbl.getHeight() / fm.getHeight();
					fm = lbl.getFontMetrics(lbl.getFont().deriveFont(fontSize));
				}
				if(fm.stringWidth(lbl.getText())>lbl.getWidth())
				{
					fontSize = fontSize * lbl.getWidth() / fm.stringWidth(lbl.getText());
					fm = lbl.getFontMetrics(lbl.getFont().deriveFont(fontSize));
				}
			}
			else if(compList[i] instanceof JTextField)
			{
				JTextField tf = (JTextField)compList[i];
				fm = tf.getFontMetrics(tf.getFont().deriveFont(fontSize));
				if(fm.getHeight()>tf.getHeight())
				{
					fontSize = fontSize * tf.getHeight() / fm.getHeight();
					fm = tf.getFontMetrics(tf.getFont().deriveFont(fontSize));
				}
				if(fm.stringWidth(tf.getText())>tf.getWidth())
				{
					fontSize = fontSize * tf.getWidth() / fm.stringWidth(tf.getText());
					fm = tf.getFontMetrics(tf.getFont().deriveFont(fontSize));
				}
			}
			else if(compList[i] instanceof JButton)
			{
				JButton btn = (JButton)compList[i];
				fm = btn.getFontMetrics(btn.getFont().deriveFont(fontSize));
				if(fm.getHeight()>btn.getHeight())
				{
					fontSize = fontSize * btn.getHeight() / fm.getHeight();
					fm = btn.getFontMetrics(btn.getFont().deriveFont(fontSize));
				}
				if(fm.stringWidth(btn.getText())>btn.getWidth())
				{
					fontSize = fontSize * btn.getWidth() / fm.stringWidth(btn.getText());
					fm = btn.getFontMetrics(btn.getFont().deriveFont(fontSize));
				}
			}
			else if(compList[i] instanceof JComboBox)
			{
				@SuppressWarnings("unchecked")
				JComboBox<String> cb = (JComboBox<String>)compList[i];
				fm = cb.getFontMetrics(cb.getFont().deriveFont(fontSize));
				if(fm.getHeight()>cb.getHeight())
				{
					fontSize = fontSize * cb.getHeight() / fm.getHeight();
					fm = cb.getFontMetrics(cb.getFont().deriveFont(fontSize));
				}
				for(int cbi=0; i<cb.getItemCount(); cbi++)
				{
					String itemText = cb.getItemAt(cbi).toString();
					if(fm.stringWidth(itemText)>cb.getWidth())
					{
						fontSize = fontSize * cb.getWidth() / fm.stringWidth(itemText);
						fm = cb.getFontMetrics(cb.getFont().deriveFont(fontSize));
					}
				}
			}
		}
		for(int i=0;i<compList.length;i++)
		{
			compList[i].setFont(compList[i].getFont().deriveFont(fontSize));
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==okButton)
		{
			fireSetupEvent(new SetupEvent(this, null, MESSAGE.OK));
		}
		if(e.getSource()==cancelButton)
		{
			fireSetupEvent(new SetupEvent(this, null, MESSAGE.CANCEL));
		}
		if(e.getSource()==setupButton)
		{
			fireSetupEvent(new SetupEvent(this, null, MESSAGE.SETUP));
		}
	}

	public void setComponentsBounds() {
		okButton.setBounds((int)(this.getWidth()*0.78), (int)(this.getHeight()*0.8), 
				(int)(this.getWidth()*0.2), (int)(this.getHeight()*0.1));	
		cancelButton.setBounds((int)(this.getWidth()*0.02), (int)(this.getHeight()*0.8), 
				(int)(this.getWidth()*0.2), (int)(this.getHeight()*0.1));	
		setupButton.setBounds((int)(this.getWidth()*0.4), (int)(this.getHeight()*0.8), 
				(int)(this.getWidth()*0.2), (int)(this.getHeight()*0.1));	
		buttonsLabel.setBounds((int)(this.getWidth()*0.02), (int)(this.getHeight()*0.1), 
				(int)(this.getWidth()*0.96), (int)(this.getHeight()*0.1));	
		buttonImages.setBounds((int)(this.getWidth()*0.02), (int)(this.getHeight()*0.25), 
				(int)(this.getWidth()*0.96), (int)(this.getHeight()*0.4));	
	}

	public void paintComponent(Graphics g) {
        super.paintComponent(g);       
		setComponentsBounds();
	}



	public void setOptionsButtonVisibile(boolean showOptionsButton) {
		setupButton.setVisible(showOptionsButton);
	}

}
